package com.bwie.controller;

import com.bwie.common.domain.Book;
import com.bwie.common.result.PageResult;
import com.bwie.common.result.Result;
import com.bwie.domain.BookExport;
import com.bwie.service.BookService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @className: BookController
 * @Description: TODO
 * @version: v1.８.0
 * @author: GONGWENXUE
 * @date: 2025/11/10 11:21
 */
@Controller
@RestController
@RequestMapping ("book")
public class BookController {
    @Autowired
    BookService bookService;
    /**
     *书籍列表
     */
    @PostMapping("bookList")
    public Result<PageResult<Book>> bookList(@RequestBody Book book){
        PageHelper.startPage(book.getPageNum(),book.getPageSize());
        List<Book> bookList=bookService.bookList(book);
        PageInfo<Book> bookPageInfo = new PageInfo<>(bookList);
        return PageResult.toResult(bookPageInfo.getTotal(),bookList);
    }
    /**
     * 导入
     */
    @PostMapping("Import")
    public Result Import(MultipartFile file){
        bookService.Import(file);
        return Result.success();
    }
    /**
     * 预览展示
     */
    @PostMapping("getPreview")
    public Result<PageResult<BookExport>> getPreview(@RequestBody BookExport bookExport){
        //	预览数据可以进行正常的分页操作
        PageHelper.startPage(bookExport.getPageNum(),bookExport.getPageSize());
        List<BookExport> bookExports=bookService.getPreview(bookExport);
        PageInfo<BookExport> bookExportPageInfo = new PageInfo<>(bookExports);
        return PageResult.toResult(bookExportPageInfo.getTotal(),bookExports);
    }
    /**
     * 查重页面
     */
    @PostMapping("getChecking")
    public Result<PageResult<BookExport>> getChecking(@RequestBody BookExport bookExport){
        //	预览数据可以进行正常的分页操作
        PageHelper.startPage(bookExport.getPageNum(),bookExport.getPageSize());
        List<BookExport> bookExports=bookService.getChecking(bookExport);
        PageInfo<BookExport> bookExportPageInfo = new PageInfo<>(bookExports);
        return PageResult.toResult(bookExportPageInfo.getTotal(),bookExports);
    }
}
